package com.lxyk.controller.student;

import com.github.pagehelper.PageInfo;
import com.lxyk.controller.user.ClassesController;
import com.lxyk.domain.*;
import com.lxyk.service.student.CommonService;
import com.lxyk.service.user.ClassesService;
import com.lxyk.service.user.UserService;
import com.lxyk.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

//普通用户

@Controller
@RequestMapping("/common")
public class CommonController {

    @Autowired
    private UserService userService;

    @Autowired
    private CommonService commonService;

    @Autowired
    private ClassesService classesService;

    //查询当前用户所带所有班级
    @RequestMapping("/findAllClasses")
    public String findAllClasses(Model model) {
        //得到当前用户用户名
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        //根据用户名查询用户信息
        SysUser user = userService.findByName(username);
        //根据用户id查询对应班级
        List<Classes> list = classesService.findByUser(user.getId());
        model.addAttribute("list", list);
        return "student_base";
    }

    //根据班级id查询班级学生信息
    @RequestMapping("/findByCid")
    public String findStudentByCid(Model model, Integer cid, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageUtils.page(pageNum, pageSize);
        PageInfo<StudentVO> pageInfo = commonService.findStudentByCid(cid, pageNum, pageSize);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("cid", cid);
        return "student_list";
    }

    //根据班级id查询对应班级基本信息
    @RequestMapping("/findClass")
    public String findClassByCid(Model model, Integer cid) {
        StudentVO student = commonService.findClassByCid(cid);
        model.addAttribute("cid", cid);
        model.addAttribute("student", student);
        return "student_add";
    }

    //根据条件模糊查询学生信息
    @RequestMapping("/condition")
    public String findStudentByCondition(Model model, StudentVO vo, Integer cid, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageUtils.page(pageNum, pageSize);
        PageInfo<StudentVO> pageInfo = commonService.findStudentByCondition(vo, pageNum, pageSize);
        pageInfo.setPageSize(pageSize);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("cid", cid);
        model.addAttribute("name", vo.getName());
        model.addAttribute("enrollTime", vo.getEnrollTime());
        model.addAttribute("consultTeacher", vo.getConsultTeacher());
        return "student_list";
    }

    //学生添加按钮跳转到添加页面
    @RequestMapping("/addSkip")
    public String addSkip(Integer cid, HttpServletRequest request) {
        request.setAttribute("cid", cid);
        return "forward:/common/findClass";
    }

    //添加学生信息
    @RequestMapping("/add")
    public String add(StudentVO vo) {
        commonService.addStudent(vo);
        //同步班级学生人数
        classesService.joinClass(vo.getCid());
        return "forward:/common/findByCid";
    }

    //根据学生id查询学生信息
    @RequestMapping("/findById")
    public String findStudentById(Model model, Integer id) {
        StudentVO student = commonService.findStudentById(id);
        model.addAttribute("student", student);
        return "student_update";
    }

    //修改学生信息
    @RequestMapping("/update")
    public String updateStudent(Student student, Classes classes) {
        commonService.updateStudent(student);
        classesService.update(classes);
        return "forward:/common/findByCid";
    }

    //根据学生id查询学生详情
    @RequestMapping("/findDetailById")
    public String findDetailById(Model model, Integer id) {
        StudentVO student = commonService.findDetailById(id);
        model.addAttribute("student", student);
        return "student_detail";
    }
}
